package com.gaogzhen.designPattern.guardedSuspension;

import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
import java.util.List;

/**
 * 测试类
 */
@Slf4j(topic = "d.GuardedTest01")
public class GuardedTest01 {
    public static void main(String[] args) {
        // 线程通信对象
        GuardedObject object = new GuardedObject();
        // 线程1需要等待线程2下载完毕
        new Thread(() -> {
            // 阻塞式等待结果
            List<String> list = (List<String>) object.get();
            log.debug("返回list大小：{}", list.size());
        }, "t1").start();

        // 线程2执行下载任务
        new Thread(() -> {
            List<String> list = null;
            // 执行下载任务
            log.debug("开始下载...");
            try {
                list = DownLoader.download("https://www.baidu.com");
            } catch (IOException e) {
                e.printStackTrace();
            }

            object.set(list);
        }, "t2").start();
    }
}
